package com.flyduck.vueshop.product.remote.service.impl;

import com.flyduck.vueshop.common.core.exception.BusinessException;
import com.flyduck.vueshop.common.core.utils.BeanUtils;
import com.flyduck.vueshop.product.entity.AppSkuStock;
import com.flyduck.vueshop.product.manager.AppSkuStockManager;
import com.flyduck.vueshop.product.mapper.AppProductMapper;
import com.flyduck.vueshop.product.mapper.AppSkuStockMapper;
import com.flyduck.vueshop.product.remote.service.RemoteSkuStockService;
import com.flyduck.vueshop.product.vo.RemoteSkuStockReduceReqVO;
import com.flyduck.vueshop.product.vo.RemoteSkuStockReleaseReqVO;
import com.flyduck.vueshop.product.vo.RemoteSkuStockRespVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * RemoteSkuStockServiceImpl
 * </p>
 *
 * @author flyduck
 * @since 2024-07-15
 */
@Service
public class RemoteSkuStockServiceImpl implements RemoteSkuStockService {

    @Resource
    private AppSkuStockMapper appSkuStockMapper;
    @Resource
    private AppProductMapper appProductMapper;
    @Resource
    private AppSkuStockManager appSkuStockManager;

    /**
     * 遍历订单明细减库存
     * 如果是单规格的商品：减product
     * 如果是多规格的商品：减sku_stock
     * @param remoteSkuStockReduceReqVOList 订单明细
     */
    @Override
    public void batchReduceSkuStock(List<RemoteSkuStockReduceReqVO> remoteSkuStockReduceReqVOList) {
        for (RemoteSkuStockReduceReqVO skuStockReduceReqVO : remoteSkuStockReduceReqVOList) {
            int count;
            if(skuStockReduceReqVO.getSkuId() == -1){
                //默认规格
                count = appProductMapper.reduceStock(skuStockReduceReqVO.getProductId(),skuStockReduceReqVO.getQuantity());
            }else {
                //多规格
                count = appSkuStockMapper.reduceStock(skuStockReduceReqVO.getSkuId(),skuStockReduceReqVO.getQuantity());
            }
            if(count == 0){
                throw new BusinessException("库存不足");
            }
        }
    }

    /**
     * 遍历订单明细释放库存
     * 如果是单规格的商品：加product
     * 如果是多规格的商品：加sku_stock
     * @param remoteSkuStockReleaseReqVOList 订单明细
     */
    @Override
    public void batchReleaseSkuStock(List<RemoteSkuStockReleaseReqVO> remoteSkuStockReleaseReqVOList) {
        for (RemoteSkuStockReleaseReqVO remoteSkuStockReleaseReqVO : remoteSkuStockReleaseReqVOList) {
            int count;
            if(remoteSkuStockReleaseReqVO.getSkuId() == -1){
                //默认规格
                count = appProductMapper.releaseStock(remoteSkuStockReleaseReqVO.getProductId(),remoteSkuStockReleaseReqVO.getQuantity());
            }else {
                //多规格
                count = appSkuStockMapper.releaseStock(remoteSkuStockReleaseReqVO.getSkuId(),remoteSkuStockReleaseReqVO.getQuantity());
            }
            if(count == 0){
                throw new BusinessException("释放库存失败");
            }
        }
    }

    @Override
    public RemoteSkuStockRespVO getSkuStock(Long productId, Long skuId) {
        AppSkuStock appSkuStock;
        if(skuId != -1){
            //多规格库存
            appSkuStock = appSkuStockMapper.selectById(skuId);

        }else {
            //单规格库存
            appSkuStock = appSkuStockManager.getSingleSkuStock(productId);
        }
        return BeanUtils.toBean(appSkuStock,RemoteSkuStockRespVO.class);
    }
}
